package controller

import (
	com "gostore/common"
	"gostore/dao/system"
	"gostore/model"
	"gostore/router"
	"strconv"

	"github.com/gin-gonic/gin"
)

type RoleControllerT struct{}

var RoleController = RoleControllerT{}

func (RoleControllerT) List(c *gin.Context) {
	page, err1 := strconv.Atoi(c.Query("page"))
	if err1 != nil {
		page = 1
	}

	limit, err2 := strconv.Atoi(c.Query("limit"))
	if err2 != nil {
		limit = 6
	}
	roles := []model.SystemRole{}
	_, err := system.RoleDao.List(&roles, page, limit)
	if err != nil {
		com.Err(c, 1, err.Error())
		return
	}

	com.Ok(c, roles)
}

func (o RoleControllerT) AddNewRoutes() {
	router.AddRouteGroup("api/role/", []router.Route{
		router.Route{
			Method:  router.MGet,
			Path:    "",
			Handler: o.List,
		},
	})
}
